<template>
    <el-dialog v-model="dialogFormVisible" :title="title" @close="closeDialog">
        <el-tree ref="tree" :data="allTeacherList" show-checkbox node-key="value" :props="defaultProps"
            @check="handleCheckChange" :default-checked-keys="keys" :model-value="keys" />
        <template #footer>
            <span class="dialog-footer">
                <!-- <el-button @click="cancel()">取消</el-button> -->
                <el-button type="primary" @click="cancel()">确认</el-button>
            </span>
        </template>
    </el-dialog>
</template>

<script>
import { ref } from "vue";
import { httpService } from "@/utils/httpService";
import { ElMessage, ElForm, ElMessageBox } from "element-plus";
import { API_BASE_NAME2, API_BASE_NAME3, } from '../../constants';

export default {
    name: "AddBaseStudent",
    components: { ElForm, ElMessage, ElMessageBox },
    data() {
        return {
            campusName: API_BASE_NAME2,
            campusName1: API_BASE_NAME3,
            dialogFormVisible: ref(false),
            formLabelWidth: "120px",
            title: "",
            personnelType: "",
            allTeacherList: [],
            defaultProps: {
                children: 'children',
                label: 'label'
            },
            keys: [],
            queryId: '',

        };
    },
    methods: {
        closeDialog() {
            this.dialogFormVisible = false
            // this.clear();
        },
        getListmy() {
            httpService("/javaApi/baseTeacherDepartment/teacherTree", {}, "get").then(
                (data) => {
                    const arr = this.editData(data)
                })
        },
        editData(valueData) {
            setTimeout(() => {
                const convertedData = [];
                for (const schoolKey in valueData) {
                    const school = valueData[schoolKey];
                    const schoolObject = {
                        label: school.name,
                        value: schoolKey,
                        children: [],
                    };
                    for (const departmentKey in school.departments) {
                        const department = school.departments[departmentKey];
                        const departmentObject = {
                            label: departmentKey,
                            value: department.departmentId,
                            departmentName: department.departmentName,
                            departmentId: department.departmentId,
                            children: [],
                        };
                        department.teachers.forEach(teacher => {
                            const teacherObject = {
                                teacherName: teacher.teacherName,
                                teacherNumber: teacher.teacherNumber,
                                label: teacher.teacherName,
                                value: teacher.teacherNumber,
                            };
                            departmentObject.children.push(teacherObject);
                        });
                        schoolObject.children.push(departmentObject);
                    }
                    convertedData.push(schoolObject);
                }
                convertedData.unshift({
                    label: "全校",
                    value: "0",
                    children: []
                })
                const dataArr = JSON.stringify(convertedData)
                this.allTeacherList = convertedData
                this.allList = dataArr
            }, 0);
        },
        lookTeacher(id) {
            httpService(
                "/javaApi/messageRecipients/list/1/2000",
                { messageId: id }, "get"
            ).then((data) => {
                // console.log(data.list);
                const departArr = []
                const teacherArr = []
                if (data.list) {
                    data.list.forEach(item => {
                        // console.log(1111,item);
                        if (item.departmentId != null) {
                            departArr.push(item.departmentId)
                        } else if (item.teacherNumber != null) {
                            teacherArr.push(item.departmentId)
                        } else if (item.departmentId != null && item.teacherNumber != null) {
                            departArr = []
                            teacherArr = []
                        }
                    });
                    // console.log("key", departArr, teacherArr);
                    this.keys = [...departArr, ...teacherArr]
                    // console.log("key",this.keys);
                    // setTimeout(function () {
                    //     this.keys.forEach((value) => {
                    //         that.$refs.tree.setChecked(value, true, false)
                    //     })
                    // }, 0);
                }
            });
        },
    },
};
</script>
<style>
.el-select {
    width: 100%;
}

.el-dialog__header {
    padding: var(--el-dialog-padding-primary);
    padding-bottom: 10px;
    border-bottom: 1px solid #dedede;
    margin-bottom: 10px;
}
</style>